﻿using UnityEngine;
using System.Linq;
using ETModel;
using System.Collections.Generic;

namespace ETModel {
    /// <summary>
    /// 给挂载Animator组件的对象
    /// </summary>
    public class BarTrailEffectController : MonoBehaviour {
        /// <summary>
        /// 节点名字:效果组件
        /// </summary>
        private Dictionary<string, BarTrailEffect> trails;

        void Awake() {
            var list = GetComponentsInChildren<BarTrailEffect>();
            trails = new Dictionary<string, BarTrailEffect>();
            foreach (var trail in list) {
                trails[trail.gameObject.name] = trail;

                trail.SetTime(0f, 0f, 1f);
            }
        }
        private void OnDestroy() {
            trails?.Clear();
        }
        /// <summary>
        /// 指定节点的拖轨效果开始
        /// </summary>
        /// <param name="gameObjectName"></param>
        public void GameObjectTrailStart(string gameObjectName) {
            if (!trails.TryGetValue(gameObjectName, out BarTrailEffect trail)) return;

            //设置拖尾时长
            trail.SetTime(trail.DefaultParams.trailTime, trail.DefaultParams.desiredTime, trail.DefaultParams.timeTransitionSpeed);
            //开始进行拖尾
            trail.StartTrail(trail.DefaultParams.desiredTime, 0.4f);
        }
        /// <summary>
        /// 指定节点的拖轨效果关闭
        /// </summary>
        /// <param name="gameObjectName"></param>
        public void GameObjectTrailStop(string gameObjectName) {
            if (!trails.TryGetValue(gameObjectName, out BarTrailEffect trail)) return;
            trail.ClearTrail();
        }

    }
}
